﻿using System;
using System.Collections.Generic;
using System.Text;
using agsXMPP;
using agsXMPP.protocol.client;
using agsXMPP.Collections;
using agsXMPP.protocol.iq.roster;
using System.Threading;

namespace DemoFacebookAPI
{
    class Client
    {
        static List<string> contacts = new List<string>();

        XmppClientConnection xmpp;

        Jid jidSender;

        #region XMPP callBack

        static string id = "";
        static string status = "";
        static string User = "";
        static string Body = "";

        static void xmpp_OnPresence(object sender, Presence pres)
        {
            id = pres.From.User + "@" + pres.From.Server;
            status = "" + pres.Type;

            if (status == "available")
            {
                for (int i = 0; i < contacts.Count; i++)
                {
                    if (id == contacts[i])
                    {
                        for (int n = 0; n < contacts.Count; n++)
                        {
                            if (contacts[i] == contacts[n])
                            {
                                contacts.RemoveAt(n);
                            }
                        }
                    }
                }
                contacts.Add(id);
            }
            else
            {
                for (int n = 0; n < contacts.Count; n++)
                {
                    if (id == contacts[n])
                    {
                        contacts.RemoveAt(n);
                        Engine.contact_Disconnected = n;
                    }
                }
            }
        }

        // Is raised when login and authentication is finished 
        static void xmpp_OnLogin(object sender)
        {
            //Engine.logText = ("Logged In");
        }

        //Handles incoming messages
        static void MessageCallBack(object sender,
                             agsXMPP.protocol.client.Message msg,
                             object data)
        {
            if (msg.Body != null)
            {
                User = msg.From.User;
                Body = msg.Body;
            }
        }
        //message reader
        private void Incoming_Messages(ChatForm chat)
        {
            if (chat.User == User + "@chat.facebook.com")
            {
                chat.UserMessage = User;
                chat.BodyMessage = Body;

                User = "";
            }
        }

        #endregion

        public void Login()
        {
            if (Engine.logging)
            {
                jidSender = new Jid(Engine.facebookNick);
                xmpp = new XmppClientConnection(jidSender.Server);
                try
                {
                    xmpp.Open(jidSender.User, Engine.facebookPass);
                    xmpp.OnLogin += new ObjectHandler(xmpp_OnLogin);
                }
                catch (Exception e)
                {
                }

                Engine.logged = true;
                Engine.logging = false;
            }
        }
        public void Loop()
        {
            if (Engine.logged == true)
            {
                Thread.Sleep(6000);

                if (xmpp.Authenticated == true)
                {
                    Presence p = new Presence(ShowType.chat, "Online");
                    p.Type = PresenceType.available;
                    xmpp.Send(p);

                    Engine.logged = false;
                    Engine.chatRunning = true;
                }
                else
                {
                    Engine.Loading = false;
                    Engine.logged = false;
                    Engine.chatRunning = false;

                    Engine.loggingFailed = true;
                }
            }
            if (Engine.chatRunning == true)
            {
                xmpp.OnPresence += new PresenceHandler(xmpp_OnPresence);
                Thread.Sleep(500);

                Engine.JID_Receiver = contacts;

                for (int index = 0; index < contacts.Count; index++)
                {
                    xmpp.MesagageGrabber.Add(new Jid(Engine.JID_Receiver[index]),
                                             new BareJidComparer(),
                                             new MessageCB(MessageCallBack),
                                             null);
                }

                if (Engine.SendMessage)
                {
                    xmpp.Send(new Message(new Jid(Engine.temp_JID_Receiver),
                     MessageType.chat,
                    Engine.SendMessageBody));

                    Engine.temp_JID_Receiver = "";
                    Engine.SendMessage = false;
                }
                //xmpp.Close();
            }
        }
        public void MessageDisplay(ChatForm chat)
        {
            //Red Message and Send it to the right Window UI
            Incoming_Messages(chat);
        }
    }
}
